package com.lanou.myim.presenter;

import android.widget.Toast;

import com.lanou.myim.application.BaseApplication;
import com.lanou.myim.model.FriendsModel;
import com.lanou.myim.model.FriendsModelInterface;
import com.lanou.myim.model.SortModel;
import com.lanou.myim.ui.fragment.FriendsFragmentInterface;

import java.util.List;

/**
 * Created by dllo on 16/3/3.
 */
public class FriendsPresenter implements FriendsPresenterInterface{
    private FriendsModelInterface friendsModel;
    private FriendsFragmentInterface friendsFragmentInterface;
    public FriendsPresenter(FriendsFragmentInterface friendsFragmentInterface){
        this.friendsFragmentInterface = friendsFragmentInterface;
        friendsModel = new FriendsModel();
    }

    @Override
    public List<SortModel> getFriendList() {
        return friendsModel.getFriendList();
    }

    @Override
    public void searchFriends(String str) {
        friendsFragmentInterface.refreshList(friendsModel.getFilterFriendList(str));
    }

    @Override
    public void receiveApply(String name, String reason) {
        friendsModel.insertData(name, reason, false, new OnCompletedListener() {
            @Override
            public void onCompleted() {
                friendsFragmentInterface.showRed();
                Toast.makeText(BaseApplication.applicationContext, "接到加好友请求", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError() {

            }
        });
    }

    @Override
    public void deleteFriend(final String userName) {
        friendsModel.deleteFriend(userName, new OnCompletedListener() {
            @Override
            public void onCompleted() {
                friendsModel.deleteFriend(userName, new OnCompletedListener() {
                    @Override
                    public void onCompleted() {
                        friendsFragmentInterface.refreshList(getFriendList());
                    }

                    @Override
                    public void onError() {

                    }
                });
                Toast.makeText(BaseApplication.applicationContext, "好友已删除", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError() {

            }
        });
    }

    @Override
    public void deletedByFriend() {
        friendsFragmentInterface.refreshList(friendsModel.getFriendList());
        Toast.makeText(BaseApplication.applicationContext, "被好友删除", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void friendIncreased(List<String> names) {
        friendsFragmentInterface.refreshList(friendsModel.getAddFriendList(names));
        Toast.makeText(BaseApplication.applicationContext, "好友数增加了", Toast.LENGTH_SHORT).show();
    }


    @Override
    public void addFriendToBlackList(String userName, Boolean flag) {
        friendsModel.addFriendToBlackList(userName, flag, new OnCompletedListener() {
            @Override
            public void onCompleted() {
                friendsFragmentInterface.refreshList(getFriendList());
                Toast.makeText(BaseApplication.applicationContext, "该好友已移到黑名单", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError() {

            }
        });
    }

    @Override
    public void applySuccessed(String userName) {
        friendsFragmentInterface.refreshList(getFriendList());
        Toast.makeText(BaseApplication.applicationContext,userName+"接受了你的好友请求", Toast.LENGTH_SHORT).show();
    }

}
